package com.ywgt.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 多线程开发，线程是一个单独的资源类，没有其他附属的操作
 * 1.属性 2.方法
 */
public class SaleTicketDemo {
    public static void main(String[] args) {
        Ticket ticket=new Ticket();
        new Thread(()-> {
            for (int i=0;i<100;i++) {
                ticket.sale();
            }
        },"A").start();
        new Thread(()-> {
            for (int i=0;i<100;i++) {
                ticket.sale();
            }
        },"B").start();
        new Thread(()-> {
            for (int i=0;i<100;i++) {
                ticket.sale();
            }
        },"C").start();

    }
}


class Ticket{
    private Integer num=100;
    private String destination;
    Lock lock=new ReentrantLock(true);
    public void sale(){
        lock.lock();
        lock.tryLock();
        try {
            if (num>0){
                System.out.println(Thread.currentThread().getName()+"剩余票数"+num--);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }



    }
}